#include "board.h"


extern float aaa;
volatile u16 ledtimer;
void NVIC_Configuration(void)
{
  __disable_irq();
  //优先级，无优先级分组
  NVIC_SetPriority(GPIOA_IRQn, 1);
  //UARTx中断使能
  NVIC_EnableIRQ(GPIOA_IRQn);
  NVIC_EnableIRQ(BTIM1_IRQn);
  //风扇的PWM
  NVIC_EnableIRQ(GTIM1_IRQn);
  NVIC_EnableIRQ(ATIM_IRQn);
  __enable_irq();
}

void Timed_end(void);
void shidu_jiance(void);

void sys_init() {
  //滴答定时器初始化
  board_init();
  //所有的终端使能
  NVIC_Configuration();
  //LED灯初始化
  LED_GPIO_init();
  //按键初始化
  Key_GPIO_init();
  btim_config();
  //蜂鸣器 需要修改
  GTIM2_CH1_config();
  //风扇pwm调节
  GTIM1_CH1_config();
  //制冷
  ZHILENG_config();
  ADC_Config();
  //5050初始化
  ws2812_init();
  HALL_GPIO_init();
  OLED_Init();
  OLED_Clear();
}
u8  yichang=0;
extern u16  cur_fan;
extern uint8_t count1111;
extern uint8_t Timed_time_flag;
extern uint8_t power_on_off;
uint16_t time_5s_delay = 0;
extern u8 key1_flag;
extern u8 butten2_led_moshi;
extern u8 key4_flag;
extern uint8_t huoer_flag;
//u32 Timed_time_1h = 60 ;//1000*60*60
extern uint8_t Timed_time;
uint32_t Timed_time_paly=0;
extern uint8_t shidu_setup;
uint8_t one_lcd =0;

void gaunji() {
  GPIO_WritePin(CW_GPIOB, GPIO_PIN_6, GPIO_Pin_SET);

  icheima_ws2812_set_color(0,0,0,0);
  icheima_ws2812_set_color(1,0,0,0);
  icheima_ws2812_set_color(2,0,0,0);
  icheima_ws2812_update();
  //蜂鸣器响声
  OLED_Clear();
  delay_1ms(6000);
  cur_fan=0;
  GTIM_SetCompare1(CW_GTIM1, cur_fan);
  power_on_off = 0;

}
u8 wudu_yichang=0;
void wudu_yichang_jiance() {
  if(wudu_yichang>3) {
    gaunji();
    wudu_yichang=0;
  }

}
int main() {
  sys_init();
  delay_1ms(10);
  while(1) {
    Key_flag_scan();
		 if(get_tv(8)>20+get_tv(9)) {
        wudu_yichang++;

      }
    if(power_on_off == 0) {
      OLED_Clear();
      continue;
    }
    if(time_5s_delay>=5000) {
      time_5s_delay = 0;
      //定时睡眠的函数
      Timed_end();
      //湿度检测函数
      shidu_jiance();
     // wudu_yichang_jiance();
    }

    LED_onoff_key();
    //按键三进行休眠并且判定的函数
    if(yichang==0) {
      huxi();
    }
    wendu_led();
    if(GPIO_ReadPin(CW_GPIOA,GPIO_PIN_10) == GPIO_Pin_RESET) {

      huoer_flag=1;
      yichang=1;
      Buzz_play();
    } else {
      huoer_flag=0;
      yichang=0;
    }
    shuiman_led();
    Timed_time_paly=Timed_time-Timer_count_get()/60/60;
    OLED_ShowString(0,0,"shidu:",16,1);
    OLED_ShowString(0,16,"wendu:",16,1);
    OLED_ShowNum(48,0,(u32)aaa,2,16,1);
    OLED_ShowNum(48,16,(u32)get_tv(9),2,16,1);
    OLED_ShowNum(72,16,(u32)get_tv(8),2,16,1);
    OLED_ShowNum(0,32,Timed_time,2,16,1);
		OLED_ShowNum(84,32,Timed_time_paly,2,16,1);
    OLED_ShowNum(32,32,shidu_setup,2,16,1);
    OLED_ShowNum(64,32,butten2_led_moshi,1,16,1);
//OLED_ShowNum(32,48,cur_fan,4,16,1);
		OLED_ShowNum(72,48,Timer_count_get(),4,16,1);
		OLED_ShowNum(0,48,Timed_time*60*60,6,16,1);
    OLED_Refresh();
    delay_1ms(100);
  }
}

uint8_t time_flag_butten=0;
void Timed_end(void) {

  if(Timed_time_flag) {
    Timed_time = 0;
    return;
  }
  if(Timed_time == 0) {
    return;
  }
  //-(Timer_count_get()/1000/60)

  //休眠时间计算
  if((( Timed_time*60*60) < Timer_count_get())) {
    Timed_time = 0;     //标志位清空
    time_flag_butten=0;
    //定时睡眠结束   关机的话 有一个关机的标志位 直接置0   还有制冷片
    gaunji();

  }

}

void shidu_jiance(void) {
  if(butten4_shidu_flag) {
    return;
  } else {
    GTIM_SetCompare1(CW_GTIM1, cur_fan);
    GPIO_WritePin(CW_GPIOB, GPIO_PIN_6, GPIO_Pin_SET);
  }
  //这里做湿度检测的地方  设定的湿度 和 实际的湿度对比 对比成功就是退出
  // 70 当前 45
  if(shidu_setup>=(int)aaa) {
    //关闭制冷片
    //power_on_off = 0;
    GTIM_SetCompare1(CW_GTIM1, 0);
    GPIO_WritePin(CW_GPIOB, GPIO_PIN_6, GPIO_Pin_RESET);
  } else {
    if(power_on_off) {
      GTIM_SetCompare1(CW_GTIM1, cur_fan);
      GPIO_WritePin(CW_GPIOB, GPIO_PIN_6, GPIO_Pin_SET);
    }
  }
}
